﻿#region Imports
using System;
using System.Drawing;
using System.Windows.Forms;
using CommonUtilities.Extensions;
#endregion

namespace CommonUtilities.Controls
{
    /// <summary>
    /// Allows sorting of data with formatted sorting symbols
    /// </summary>
    public partial class CUtil_SortDirectioner : UserControl
    {
        #region Declarations
        /// <summary>
        /// Text to show when sorting is not allowed
        /// </summary>
        const string NO_SORT = "--";

        /// <summary>
        /// Text to show when sort is ascending
        /// </summary>
        const string SORT_ASC = "▲";

        /// <summary>
        /// Text to show when sort is descending
        /// </summary>
        const string SORT_DESC = "▼";

        /// <summary>
        /// Specifies when Reset is enabled
        /// </summary>
        bool enableReset = false;

        /// <summary>
        /// EventHandler to be executed when sort direction is changed
        /// </summary>
        public event EventHandler OnSortChanged;
        #endregion

        #region Properties
        /// <summary>
        /// Specifies whether sort is ascending
        /// </summary>
        public bool IsAscending { get { return chkSortDirection.Checked; } }

        /// <summary>
        /// Specifies default sort of the control when enabled
        /// </summary>
        public bool IsDefaultSortAsc { get; private set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor
        /// <para>(initializes control and specifies size of the control)</para>
        /// </summary>
        private CUtil_SortDirectioner()
        {
            InitializeComponent();

            SetDefaultSort(true);
            lblSortDirection.MinimumSize = lblSortDirection.MaximumSize = lblSortDirection.Size
                = this.MinimumSize = this.MaximumSize = this.Size = new Size(19, 19);
        }

        /// <summary>
        /// Initializes control and specifies default sort of the control
        /// <para>(implements default ctor to initialize control and size)</para>
        /// </summary>
        /// <param name="isDefaultSortAsc">TRUE: Default sort = Ascending</param>
        public CUtil_SortDirectioner(bool isDefaultSortAsc)
            : this()
        {
            SetDefaultSort(isDefaultSortAsc);
        }
        #endregion

        #region Events
        private void CUtil_SortDirectioner_EnabledChanged(object sender, EventArgs e)
        {
            if (!this.Enabled) enableReset = true;
            lblSortDirection.Enabled = chkSortDirection.Enabled = this.Enabled;
            UpdateSortText();
        }

        private void lblSortDirection_Click(object sender, EventArgs e)
        {
            chkSortDirection.Checked = !chkSortDirection.Checked;
            UpdateSortText();

            if (!OnSortChanged.IsNull()) OnSortChanged(sender, e);
        }

        private void CUtil_SortDirectioner_Load(object sender, EventArgs e)
        {
            this.Enabled = false;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Updates/Formats the text of the sort button
        /// </summary>
        public void UpdateSortText()
        {
            if (!chkSortDirection.Enabled)
            {
                lblSortDirection.Text = NO_SORT;
                lblSortDirection.ForeColor = Color.Red;
            }
            else
            {
                if (enableReset)
                {
                    enableReset = false;
                    chkSortDirection.Checked = IsDefaultSortAsc;
                }

                switch (chkSortDirection.CheckState)
                {
                    case CheckState.Indeterminate:
                    case CheckState.Unchecked:
                        lblSortDirection.Text = SORT_DESC;
                        lblSortDirection.ForeColor = Color.Blue;
                        break;

                    case CheckState.Checked:
                    default:
                        lblSortDirection.Text = SORT_ASC;
                        lblSortDirection.ForeColor = Color.Green;
                        break;
                }

                tltipSortDirectioner.SetToolTip(lblSortDirection,
                    this.Enabled ? (IsAscending ? "Ascending" : "Descending") : "NO sort");
            }
        }

        private void SetDefaultSort(bool isDefaultSortAsc)
        {
            this.IsDefaultSortAsc = isDefaultSortAsc;
        }
        #endregion
    }
}
